﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bainx.WPF.Extend.Control
{
    /// <summary>
    /// TabItem的扩展类型,含有WebBrowser成员
    /// </summary>
    public class TabItemWebBrowser : TabItem
    {
        WebBrowserUserControl webBrowser;

        /// <summary>
        /// TabItem含有的浏览器成员
        /// </summary>
        public WebBrowserUserControl WebBrowser
        {
            get { return webBrowser; }
            set { webBrowser = value; }
        }
        /// <summary>
        /// 实例化一个含有浏览器的TabItem扩展控件
        /// </summary>
        /// <param name="Owner"></param>
        /// <param name="Style"></param>
        /// <param name="uri"></param>
        /// <param name="Header"></param>
        public TabItemWebBrowser(Window Owner, Style Style, Uri uri, object Header)
        {
            webBrowser = new WebBrowserUserControl(Owner);
            webBrowser.TabItem = this;
            this.Style = Style;
            this.Header = Header;
            webBrowser.DocumentTitleChanged+=new EventHandler(webBrowser_DocumentTitleChanged);
            webBrowser.NewWindow += new System.ComponentModel.CancelEventHandler(webBrowser_NewWindow);
            webBrowser.Uri = uri;
        }
        /// <summary>
        /// 事件:浏览器打开新窗口事件
        /// </summary>
        public event System.ComponentModel.CancelEventHandler NewWindow;

        void webBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (NewWindow != null) NewWindow(sender, e);
        }

        void webBrowser_DocumentTitleChanged(object sender, EventArgs e)
        {
            WebBrowserUserControl wbuc = sender as WebBrowserUserControl;
            if (wbuc != null)
            {
                this.Header = wbuc.DocumentTitle.Length > 10 ? wbuc.DocumentTitle.Substring(0, 10) : wbuc.DocumentTitle;
            }
        }
    }
}
